package com.dali.ashook;

import android.os.Build;

import androidx.annotation.RequiresApi;

//import com.liangchen.astools.utils.AsLogUtils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AsHook {
    private final static String TAG = "AsHook";

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void startHook() {
        hookWindowSession();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void hookWindowSession() {
        try {
            Class<?> cls = Class.forName("com.liangchen.astools.utils.AsLog");
            Method funcTestMethod = cls.getDeclaredMethod("functionTest");
            funcTestMethod.invoke(null);

            Object funcTestProxy = null;
            //funcTestProxy = Proxy.newProxyInstance(AsAppContext.getApplicationContext().getClass().getClassLoader(),
              //      cls.getInterfaces(), new ProxyHandler(objAsLog));

        } catch (ClassNotFoundException | NoSuchMethodException e) {
           // AsLogUtils.AsLog.i(TAG, "ClassNotFoundException" + e.toString());
        } catch (IllegalAccessException e) {
           // AsLogUtils.AsLog.i(TAG, "ClassNotFoundException");
        } catch (InvocationTargetException e) {
            //AsLogUtils.AsLog.i(TAG, "ClassNotFoundException");
        }
    }

    public static class ProxyHandler implements InvocationHandler {
        private Object mTarget;

        public ProxyHandler(Object object) {
            this.mTarget = object;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //AsLogUtils.AsLog.i("cdgTest","before invoke");
            Object invoke = method.invoke(mTarget, args);
            //AsLogUtils.AsLog.i("cdgTest","after invoke");
            return invoke;
        }
    }

    /*
    public static void hookWindowSession() {
        try {
            Class<?> cls = Class.forName("android.view.WindowManagerGlobal");

            Method getWindowSessionMethod = cls.getDeclaredMethod("getWindowSession");
            Object iWindowSession = getWindowSessionMethod.invoke(null);

            Field field = cls.getDeclaredField("sWindowSession");
            field.setAccessible(true);
            Object windowSessionProxy = field.get(null);
            windowSessionProxy = Proxy.newProxyInstance(NCAppContext.getAppContext().getClass().getClassLoader(),
                    windowSessionProxy.getClass().getInterfaces(), new HookedWindowSession(windowSessionProxy));
            field.set(iWindowSession, windowSessionProxy);

        } catch (ClassNotFoundException | NoSuchMethodException e) {
            HLogger.tag(TAG).e(e.toString());
        } catch (IllegalAccessException e) {
            HLogger.tag(TAG).e(e.toString());
        } catch (InvocationTargetException e) {
            HLogger.tag(TAG).e(e.toString());
        } catch (NoSuchFieldException e) {
            HLogger.tag(TAG).e(e.toString());
        }
    }

    public static class HookedWindowSession implements InvocationHandler {
        private Object mWindowSessionObj;

        public HookedWindowSession(Object windowSessionObj) {
            this.mWindowSessionObj = windowSessionObj;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object invoke = method.invoke(mWindowSessionObj, args);
            if (method.getName().equals("finishDrawing")) {

            }
            return invoke;
        }
    }
    */
}

